/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.myapp.action;

import com.myapp.metier.Helper;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.hibernate.dialect.H2Dialect;

/**
 *
 * @author user
 */
public class PassageOrdreDiffereAction extends org.apache.struts.action.Action {

    /* forward name="success" path="" */
    private static final String SUCCESS = "success";
    private static final String IPSERVEURORDER = "169.254.43.200";

    /**
     * This is the action called from the Struts framework.
     *
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        // Mesurer la charge sur le serveur de passage d'ordre

        URL url = new URL("http://" + IPSERVEURORDER + ":8080/Hibernate/MesureCharge.do");
        URLConnection yc = url.openConnection();
        BufferedReader in = new BufferedReader(
                new InputStreamReader(
                        yc.getInputStream()));
        String inputLine;
        String currentCharge = "";
        while ((inputLine = in.readLine()) != null) {
            currentCharge += inputLine;
        }
        in.close();
        int charge = Integer.valueOf(currentCharge);

        if (charge < 1000) {
            Helper helper = new Helper();
            int nbOrdreDiffere = helper.countOrderDifferes();

            if (nbOrdreDiffere > 0) {

                URL url1 = new URL("http://" + IPSERVEURORDER + ":8080/Hibernate/PassageOrdreRegional.do?nbClient=" + nbOrdreDiffere);
                URLConnection yc1 = url1.openConnection();
                BufferedReader in1 = new BufferedReader(
                        new InputStreamReader(
                                yc1.getInputStream()));
                String inputLine1;

                while ((inputLine1 = in1.readLine()) != null) {

                }
                in1.close();

            }
            Helper helper2 = new Helper();
            helper2.deleteOrderDifferes();
        }
        return mapping.findForward(SUCCESS);
    }
}
